﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11_lambda表达式_有参有返回值
{
    public delegate int OtherDel(int intParam);
    public delegate int OtherDel2(int intParam1, int intParams2);
    internal class Program
    {
        public static int Add(int x)
        {
            return x + 1;
        }
        static void Main(string[] args)
        {
            //实例化委托对象=>绑定方法
            OtherDel otherDel1 = new OtherDel(Add);
            //调用委托
            Console.WriteLine("通过实例化委托绑定:{0}",otherDel1.Invoke(1));

            //通过匿名委托绑定方法
            OtherDel otherDel2 = delegate (int x)
            {
                return x + 1;
            };
            //调用委托
            Console.WriteLine("通过匿名化委托绑定:{0}", otherDel2.Invoke(1));

            //Lambda表达式
            OtherDel otherDel3 = (int x) => { return x + 1; };
            //调用委托
            Console.WriteLine("通过Lambda表达式绑定-1:{0}", otherDel3.Invoke(1));

            OtherDel otherDel4 = (x) => { return x + 1; };
            //调用委托
            Console.WriteLine("通过Lambda表达式绑定-2:{0}", otherDel4.Invoke(1));

            OtherDel otherDel5 = x => { return x + 1; };
            //调用委托
            Console.WriteLine("通过Lambda表达式绑定-3:{0}", otherDel5.Invoke(1));

            OtherDel otherDel6 = x => x + 1;
            //调用委托
            Console.WriteLine("通过Lambda表达式绑定-4:{0}", otherDel6.Invoke(1));

            //如果委托的形参列表有且只有一个唯一的参数，那么可以省略小括号，其他所有情况下都要写小括号
            OtherDel2 otherdel1 = (int x, int y) => 
            {
                return x + y;
            };
            Console.WriteLine("通过otherDel完成调用:{0}", otherdel1.Invoke(6,2));

            // 如果与委托绑定的方法体有且只有一句那么方法体的花括号可以省略，否则其他情况下都要写花括号
            OtherDel2 otherdel2 = (x, y) => 
            {
                Console.WriteLine("{0}+{1}={2}",x,y,x+y);
                return x + y;
            };
            //Console.WriteLine("通过otherDel完成调用:{0}", otherDel2.Invoke(6, 2));


            Console.ReadKey();
        }

    }
}
